#ifndef GLCONTEXT_H
#define GLCONTEXT_H

// #ifdef _MSC_VER
// #define USEWIN32_IMPL
// #else
// #define USEEGL_IMPL
// #endif

// #define USEWIN32_IMPL 
#define USEEGL_IMPL
 

/* 显示背景 ，用来测试带背景的效果效果 */
// #define LOAD_BACKGROUND 

typedef struct tagGlContext GlContext, *PGlContext;
#ifdef __cplusplus
extern "C" {
#endif
 
#ifdef USEEGL_IMPL
#include<glad/gles2.h>
#define GLAD_CONTEXT GladGLES2Context
#endif;

#ifdef USEWIN32_IMPL
#include<glad/gl.h>
#define GLAD_CONTEXT GladGLContext
#endif

typedef GLAD_CONTEXT  *PGLAD_CONTEXT;

extern PGLAD_CONTEXT curGladContext;
/** 
 * @note
 * glcMakeCurrent 会被调用 
 **/
PGlContext glcCreateGlContext(int width, int height);
void glcDeleteGlContext(PGlContext context);
void glcMakeCurrent(PGlContext context);
void glcSwapBuf(PGlContext context);
void glcSetSize(PGlContext context,int width, int height);
void glcgetSize(PGlContext context,int *width, int *height);
unsigned int glcGetColorBuf(PGlContext context,int index);
unsigned int glcCurrentColorBuf(PGlContext context );
unsigned int glcCurrentFbo(PGlContext context);
/* 切换到下一个fbo进行绘制 */
void glcGoNextFbo(PGlContext context); 
unsigned int glcGetFbo(PGlContext context,int index);
void glcSwapFbo(PGlContext context);

/* 
* 把当前帧copy到fboIndex帧
*/
void glcSwap(PGlContext context,int fboIndex);
void glcPersent(PGlContext context);

/* 开始绘制 */
void glcBegin(PGlContext context);

void glcEnd(PGlContext context);

/**
 * @return 
 * 
 */
int glcGetStatus(PGlContext context);

/**
 * 更新后台缓存帧数据 
 * @param dataw,datah 帧宽高
 * @param data 帧数据 
 * @note
 * 
 * 建议用rgba
 * - 4字节对齐会加快一些速度
 * - 内部使用的是rgba不用进行转换
 */
void glcUpdateBuffRGBA(PGlContext context,int dataw ,int datah,void *data);

/* 返回rgba数据 */
void glcReadCurPixelsRGB(PGlContext context,void *pixel,int w,int rows);
void glcReadCurPixelsRGBA(PGlContext context,void *pixel,int w,int rows);

#ifdef USEEGL_IMPL
#include<glesdef.h>
#endif

#ifdef USEWIN32_IMPL
#include<gldef.h>
#include<glad/gl.h>
#endif 

#ifdef USEWIN32_IMPL
#define GLSL_VER  "#version 330 core\n"
#endif

#ifdef USEEGL_IMPL
#define GLSL_VER  "#version 300 es\n"
#endif

#ifndef OPENGL_RENDER_GRAPH_GL_SHADER_FORAMT
#define OPENGL_RENDER_GRAPH_GL_SHADER_FORAMT
#define	NUMBERWOLF_STRINGIZE(x)	#x
#define	NUMBERWOLF_GL_SHADER(shader) GLSL_VER NUMBERWOLF_STRINGIZE(shader)
#define	NUMBERWOLF_GL_SHADER_WITHOUTVERSION(shader) NUMBERWOLF_STRINGIZE(shader)
#endif

#ifdef __cplusplus
}
#endif

#include"stb_image_write.h"
#include"stb_image.h"

#define RR(A) (((float)((A) & 0xFF) )/256.0f)
#define GG(A) (((float)((A>>8) & 0xFF))/256.0f)
#define BB(A) (((float)((A>>16) & 0xFF))/256.0f)
#define AA(A) (((float)((A>>24) & 0xFF))/256.0f)

#endif // GLCONTEXT_H
